package com.ebspp.server.storage;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ebspp.global.General;
import com.ebspp.thrift.DataMsg;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.Relationship;
import com.sleepycat.persist.model.SecondaryKey;

@Entity
public class PersistentMsg 
{
	@PrimaryKey
	String key = null;
	@SecondaryKey(relate=Relationship.MANY_TO_MANY)
	Set<String> ts = null;
	byte[] value = null;
	
	public PersistentMsg(){
	}
	
	public PersistentMsg(String key, String ts, byte[] value)
	{
		setKey(key);
		setTs(ts);
		SetValue(value);
	}
	
	public void setKey(String key)
	{
		this.key = key;
	}
	public void setTs(String ts)
	{
		if(ts == null)
			ts = General.getTS();
		if(this.ts == null)
			this.ts = new HashSet<String>();
		this.ts.add(ts);
	}
	public void SetValue(byte[] value)
	{
		this.value = value;
	}
	
	public String getKey()
	{
		return key;
	}
	public Set<String> getTs()
	{
		return ts;
	}
	public byte[] getValue()
	{
		return value;
	}
	
	public static PersistentMsg v2p(DataMsg v)
	{
		PersistentMsg p = new PersistentMsg(v.getKey(), v.getTs(), v.getValue());
		return p;
	}
	
	public static DataMsg p2v(PersistentMsg p)
	{
		DataMsg v = new DataMsg();
		return v;
	}
	
	public boolean equals(Object obj)
	{
		if(obj == null)
			return false;
		if(obj instanceof PersistentMsg)
		{
			PersistentMsg msg = (PersistentMsg)obj;
			if(msg.key.equals(key) && msg.ts.equals(ts))
				return true;
		}
		return false;
	}
	
	public int hashCode()
	{
		return key.hashCode() * 13 + ts.hashCode() * 17;
	}
}
